package com.jzy.gulimall.member.controller;

import com.jzy.common.exception.BizCodeEnum;
import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.R;
import com.jzy.gulimall.member.entity.MemberEntity;
import com.jzy.gulimall.member.exception.PhoneException;
import com.jzy.gulimall.member.exception.UsernameException;
import com.jzy.gulimall.member.feign.CouponFeignService;
import com.jzy.gulimall.member.service.MemberService;
import com.jzy.gulimall.member.vo.MemberLoginVo;
import com.jzy.gulimall.member.vo.MemberRegisterVo;
import com.jzy.gulimall.member.vo.SocialUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;


/**
 * 会员
 *
 * @author jzy
 * @email 1802511725@qq.com
 * @date 2020-07-11 20:15:14
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Resource
    private CouponFeignService couponFeignService;

    /**
     * 微博登录 登录或注册
     */
    @PostMapping("/oauth/login")
    public R weiboLogin(@RequestBody SocialUser socialUser) {
        MemberEntity memberEntity = null;
        try {
            memberEntity = memberService.login(socialUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (memberEntity != null) {
            return R.ok().setData(memberEntity);
        }
        return R.error(BizCodeEnum.WEIBO_LOGIN_EXCEPTION.getCode(), BizCodeEnum.WEIBO_LOGIN_EXCEPTION.getMessage());
    }


    /**
     * 登录
     * 查询会员信息
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (memberEntity != null) {
            return R.ok().setData(memberEntity);
        }
        return R.error(BizCodeEnum.LOGIN_EXCEPTION.getCode(), BizCodeEnum.LOGIN_EXCEPTION.getMessage());
    }

    /**
     * 注册
     * 保存会员信息
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo memberRegisterVo) {
        try {
            memberService.register(memberRegisterVo);
            return R.ok();
        } catch (UsernameException e) {
            // 2020/9/17 注册失败 按照需求返回注册错误信息
            return R.error(BizCodeEnum.USERNAME_EXIST_EXCEPTION.getCode(), BizCodeEnum.USERNAME_EXIST_EXCEPTION.getMessage());
        } catch (PhoneException e) {
            // 2020/9/17 注册失败 按照需求返回注册错误信息
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
        }
    }

    /**
     * 测试
     * 会员优惠券信息
     */
    @RequestMapping("/coupons")
    public R memberCoupon() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R memberCoupons = couponFeignService.memberCoupon();
        return Objects.requireNonNull(R.ok().put("member", memberEntity)).put("coupons", memberCoupons.get("memberCoupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
